<template>
	<view>
	<view class="header">	
	<view class="userImg" @click="login()">
	<image :src="avatarUrl"></image>
	</view>
	<!--称昵-->
	<view class="userName">{{nickName}}</view>

	</view>
	<view><button @click="Exit()">退出登录</button> </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				openid: '',
				nickName:"请先登录",
				avatarUrl:"../../static/home.png",
			}
		},
		onShow() {
			uni.getStorage({
				key:'avatarUrl',
				success:(res)=>{
					console.log(res);
					this.avatarUrl=res.data;
				},
				fail:function(res){
					console.log(res);
				}
			}),
			uni.getStorage({
				key:'nickname',
				success:(res)=>{
					console.log(res.data);
					this.nickName=res.data;
				},
				fail:function(res){
					console.log(res);
				}
			})
		},
		methods: {
			login(){
				uni.getProvider({
				    service: 'oauth',
				    success: function (res) {
				        if (~res.provider.indexOf('weixin')) {
				            uni.login({
				                provider: 'weixin',
				                success: function(loginRes) {
									uni.getUserInfo({
										provider:'weixin',
										success:function(infoRes){
											console.log(infoRes);
											//this.nickName=infoRes.userInfo.nickName;
											uni.setStorage({
												key:'nickname',
												data:infoRes.userInfo.nickName,
												success:function(){
													console.log("setName success!");
												}
											}),
											uni.setStorage({
												key:'avatarUrl',
												data:infoRes.userInfo.avatarUrl,
												success:function(){
													console.log("setAvatarUrl success!");
												}
											})
										}
									}),
									uni.setStorage({
										key:'openid',
										data:loginRes.authResult.openid,
										//data:'426DB83C595799757D0C6D5E1DE12F23',
										success:function(){
											uni.switchTab({
												url: '../index/index',
												success: res => {
													 console.log("跳转成功")
												},
												fail:function(res){
													console.log("跳转失败")
												}
												})	
										},
										fail:function(res){
											console.log("本地储存失败");
										},
										complete:function(res){
											console.log("登录成功");
										}
									})
									
				                }
				            })
				        }
				    }
				});
			},
			// loginBylocal(){
			// 	uni.setStorage({
			// 		key:'openid',
			// 		//data:loginRes.authResult.openid,
			// 		data:'426DB83C595799757D0C6D5E1DE12F23',
			// 		success:function(){
			// 			uni.switchTab({
			// 				url: '../index/index',
			// 				success: res => {
			// 					// console.log("跳转成功")
			// 				},
			// 				fail:function(res){
								
			// 				}
			// 				})
								
							
			// 		},
			// 		fail:function(res){
			// 			console.log("本地储存失败");
			// 		},
			// 		complete:function(res){
			// 			console.log("登录成功");
			// 		}
			// 	})
			// },
			
			Exit(){
				uni.clearStorage();
				this.avatarUrl="../../static/home.png"
				this.nickName="请先登录"
				console.log("退出登录");
			}
			
		},
	}
</script>
<style>
	.header{
	  background-color: #28B298;
	  padding: 30rpx 0;
	}
	.header .userImg{
	  width: 150rpx;
	  height: 150rpx;
	  background-color: #fff;
	  border-radius: 50%;
	  margin: 0 auto;
	  line-height: 150rpx;
	  font-weight: bold;
	  font-size: 40rpx;
	  text-align: center;
	}
	.header .userImg image{
	  width: 100%;
	  height: 100%;
	  border-radius: 50%;
	}
	.header .userName{
	  color:#E6E6E6 ;
	  text-align: center;
	  font-size: 36rpx;
	  font-weight: bold;
	  margin-top: 20rpx;
	}
</style>